<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

//use OpenAI\Laravel\Facades\OpenAI;

use OpenAI\Laravel\Facades\OpenAI as LaraOpenAI;

class OpenaiController extends Controller
{

    protected $api_key;
    protected $client;

    public function __construct()
    {
        $this->api_key = getenv('OPENAI_API_KEY');
        $this->client  = \OpenAI::client($this->api_key);
    }

    function list()
    {
        $response = $this->client->models()->list();
        echo "<pre>";
        var_export($response->toArray());
    }


    function client()
    {
        $yourApiKey = getenv('OPENAI_API_KEY');

        $client = \OpenAI::client($yourApiKey);


        $result = $client->chat()->create([
            'model'    => 'gpt-3.5-turbo',
            'messages' => [
                ['role' => 'user', 'content' => 'What\'s the weather like in Boston?'],
            ],
        ]);
        dump($result);

        echo $result->choices[0]->message->content; // Hello! How can I assist you today?
    }

    //根据提示创建图片
    function imageCreate()
    {
        $response = $this->client->images()->create([
            'model'           => 'dall-e-2',//dall-e-3 不能用，有速率限制
            'prompt'          => 'A cute baby sea otter',
            'n'               => 1,
            'size'            => '1024x1024',
            'response_format' => 'url',
        ]);
        dump($response);

    }

    //两张图片结合起来
    function imageUpdate()
    {
        $response = $this->client->images()->edit([
            'image'           => fopen(storage_path('image/1.png'), 'r'),
            'mask'            => fopen(storage_path('image/2.png'), 'r'),
            'prompt'          => '苹果和仙人掌组合起来',
            'n'               => 1,
            'size'            => '256x256',
            'response_format' => 'url',
        ]);
        dd($response);
    }

    //创建给定图像的变体。
    function imageVariation()
    {
        $response = $this->client->images()->variation([
            'image' => fopen(storage_path('image/1.png'), 'r'),
            'n' => 2,
            'size' => '256x256',
            'response_format' => 'url',
        ]);
        dd($response);
    }

}
